package com.itheima.mArrayList;

import java.util.ArrayList;

/*
      集合试写
      ArrayList构造方法:
      ArrayList()构造一个初始容量为 10 的空列表

      成员方法:
          添加:
               boolean add(E e)  将指定的元素添加到此列表的尾部
               void add(int index,E element)将指定的元素插入此列表中的指定位置.

 */
public class ArrayListDemo01 {
    public static void main(String[] args) {
        ArrayList<Student> List = new ArrayList<>();
        //增
        Student stu1 = new Student("张三", 4);
        List.add(stu1);
        Student stu2 = new Student();
        stu2.setName("李四");
        stu2.setAge(45);
        List.add(stu2);
        Student stu3 = new Student("王五", 15);
        List.add(stu3);
        for (int i = 0; i < List.size(); i++) {
            Student stu = List.get(i);
            System.out.println(stu.getName());
            System.out.println(stu.getAge());
        }
        System.out.println("------------");
        //删
        List.remove(1);
        for (int i = 0; i < List.size(); i++) {
            Student stu = List.get(i);
            System.out.println(stu.getName());
            System.out.println(stu.getAge());
        }
        System.out.println("------------");
        //改
        Student stu4 = new Student("孙六", 5);
        List.set(0, stu4);
        for (int i = 0; i < List.size(); i++) {
            Student stu = List.get(i);
            System.out.println(stu.getName());
            System.out.println(stu.getAge());
        }
        System.out.println("-------------");
        //查
        Student stu5 = List.get(0);
        System.out.println(stu5.getName());
        System.out.println(stu5.getAge());
        System.out.println("-------------");
        //筛
        ArrayList<Student> newList = getStudent(List);
        for (int i = 0; i < newList.size(); i++) {
            Student stu = newList.get(i);
            System.out.println(stu.getName());
            System.out.println(stu.getAge());
        }

    }

    //筛选对象
    public static ArrayList<Student> getStudent(ArrayList<Student> List) {
        ArrayList<Student> newList = new ArrayList<>();
        for (int i = 0; i < List.size(); i++) {
            Student stu = List.get(i);
            if (stu.getAge() < 10) {
                newList.add(stu);
            }
        }
        return newList;
    }
}